package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.mapper.CheckItemMapper;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import com.itheima.entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = CheckItemService.class)
public class CheckItemServiceImlp implements CheckItemService {

    @Autowired
    private CheckItemMapper checkItemMapper;

    @Override
    @Transactional
    public void addCheckItem(CheckItem checkItem) {
       checkItemMapper.addCheckItem(checkItem);
    }


    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {

        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<CheckItem> page = checkItemMapper.findByCondition(queryPageBean.getQueryString());

        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void delete(Integer id) throws  RuntimeException{
        long countByCheckItemId = checkItemMapper.findCountByCheckItemId(id);
        System.out.println(countByCheckItemId);
        if (countByCheckItemId>0){
            throw  new RuntimeException("当前检查被引用不能删除");
        }else {
            checkItemMapper.delById(id);
        }
    }

    @Override
    public CheckItem findById(int id) {
        CheckItem checkItem=checkItemMapper.findById(id);
        return checkItem;
    }

    @Override
    public void editCheckItem(CheckItem checkItem) {
        checkItemMapper.edit(checkItem);
    }

    @Override
    public List<CheckItem> findAll() {
        List<CheckItem> checkItems= checkItemMapper.findAll();
        return checkItems;
    }


}
